/****************************************************
	文件：ClassPoolMgr.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/8/26 11:6:32
	功能：对象池管理器
*****************************************************/

using System;
using System.Collections.Generic;

namespace HTUtility
{
    public class ClassPoolMgr : Singleton<ClassPoolMgr>
    {
        /// <summary>
        /// 对象池缓存字典
        /// </summary>
        private Dictionary<Type, object> mPoolDict = new Dictionary<Type, object>();

        /// <summary>
        /// 获取或创建类对象池
        /// maxCount：类对象池的最大池容量，若小于等于0则不限制容量
        /// </summary>
        public ClassObjectPool<T> GetOrCreateObjectPool<T>(int maxCount) where T : class, new()
        {
            object pool = null;
            Type key = typeof(T);
            if (mPoolDict.TryGetValue(key, out pool))
            {
                if (pool == null)
                {
                    pool = new ClassObjectPool<T>(maxCount);
                }
            }
            else
            {
                pool = new ClassObjectPool<T>(maxCount);
                mPoolDict.Add(key, pool);
            }
            return pool as ClassObjectPool<T>;
        }
    }
}
